﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SES.CMS.DO;
using SES.CMS.BL;

namespace SES.CMS.AdminCP.PageUC
{
    public partial class ucProductCategory : System.Web.UI.UserControl
    {
        cmsLoaiSanPhamDO objLSanPham = new cmsLoaiSanPhamDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            txtTenLSanPham.Focus();
            Functions.ddlDatabinder(ddlParentID, cmsLoaiSanPhamDO.LOAISANPHAMID_FIELD, cmsLoaiSanPhamDO.TENLOAISANPHAM_FIELD, new cmsLoaiSanPhamBL().SelectAll(0));
            if (Request.QueryString["PID"] != null)
            {
                objLSanPham.LoaiSanPhamID = int.Parse(Request.QueryString["PID"].ToString());
                InitForm();
            }
        }

        protected void InitForm()
        {
            objLSanPham = new cmsLoaiSanPhamBL().Select(objLSanPham);
            txtTenLSanPham.Text = objLSanPham.TenLoaiSanPham.ToString();
            txtProductCategoryName.Text = objLSanPham.ProductTypeName.ToString();
            txtMoTa.Text = objLSanPham.MoTa.ToString();
            txtDescription.Text = objLSanPham.Description.ToString();
            chkIsMainMenu.Checked = objLSanPham.IsMainMenu;
            txtOrderID.Text = objLSanPham.OrderID.ToString();
            if (!string.IsNullOrEmpty(objLSanPham.HinhAnh))
            {
                hplImage.Visible = true;
                hplImage.NavigateUrl = "~/Media/" + objLSanPham.HinhAnh;
            }
            try
            {
                ddlParentID.SelectedValue = objLSanPham.ParentID.ToString();
            }
            catch
            { }
        }

        protected void SetObject()
        {
            objLSanPham.TenLoaiSanPham = txtTenLSanPham.Text;
            objLSanPham.ProductTypeName = txtProductCategoryName.Text;
            objLSanPham.MoTa = txtMoTa.Text;
            objLSanPham.Description = txtDescription.Text;
            objLSanPham.IsMainMenu = chkIsMainMenu.Checked;
            objLSanPham.OrderID = int.Parse(txtOrderID.Text);
            int PID = int.Parse(ddlParentID.SelectedValue);
            if (PID == 0 || PID == objLSanPham.LoaiSanPhamID)
            {
                objLSanPham.ParentID = 0;
                objLSanPham.RootLevel = 1;
                objLSanPham.RelationValue = FriendlyUrl(txtTenLSanPham.Text).Substring(0,FriendlyUrl(txtTenLSanPham.Text).LastIndexOf("."));
            }
            if (PID > 0 && PID != objLSanPham.LoaiSanPhamID)
            {
                objLSanPham.ParentID = PID;
                DataTable dtLSP = new cmsLoaiSanPhamBL().SelectParentIDToTable(PID, 0);
                objLSanPham.RootLevel = dtLSP.Rows.Count;
                int LoaiSPID = 0;
                if (dtLSP.Rows.Count == 1)
                {
                    LoaiSPID = int.Parse(ddlParentID.SelectedValue);
                   
                }
                if (dtLSP.Rows.Count > 1)
                {
                    LoaiSPID = int.Parse(dtLSP.Rows[1]["ParentID"].ToString());
                }
                objLSanPham.RelationValue = new cmsLoaiSanPhamBL().SelectByPK(LoaiSPID).Rows[0]["RelationValue"].ToString();

            }
            if (!string.IsNullOrEmpty(fulImage.FileName))
            {
                string FileName = string.Format("{0}{1}", fulImage.FileName.Substring(0, fulImage.FileName.LastIndexOf(".")) + "-" + DateTime.Now.ToString("dd-MM-yyyy-hhmmss"), fulImage.FileName.Substring(fulImage.FileName.LastIndexOf(".")));
                string SaveLocation = string.Format("{0}\\{1}", Server.MapPath("~/Media/"), FileName);
                fulImage.SaveAs(SaveLocation);
                objLSanPham.HinhAnh = FileName;
            }
        }

        public string FriendlyUrl(string s)
        {
            return Ultility.Change_AV(s);
        }
        long i;
        protected void btnSave_Click(object sender, EventArgs e)
        {
            SetObject();
            if (objLSanPham.LoaiSanPhamID <= 0)
            {
                i = new cmsLoaiSanPhamBL().Insert(objLSanPham);
                Functions.Alert("Thêm mới thành công", "Default.aspx?Page=ListProductCategory");
            }
            else
            {
                i = new cmsLoaiSanPhamBL().Update(objLSanPham);
                i = objLSanPham.LoaiSanPhamID;
                Functions.Alert("Cập nhật thành công", "Default.aspx?Page=ListProductCategory");
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx?Page=ListProductCategory");

        }
    }
}